package breeze.pixel.weather.earthquake.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;
import brz.breeze.tool_utils.Blog;

public class EarthquakeActivity extends BaseView {

    private String TAG = EarthquakeActivity.class.getSimpleName();

    private ListView earth_listview;

    private MTextView BigTitle;

    private List<String> datas;

    private List<String> names;
    
    private final List<String> levels=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setContentView(R.layout.activity_earthquake_view);
        earth_listview=findViewById(R.id.activity_earthquake_viewListView);
        BigTitle=findViewById(R.id.earthquake_bigtitle);
        BigTitle.setText("今日地震");
        getEarthquakeDatas();
    }

    @Override
    public void init() {

    }

    @Override
    public void initData() {

    }

    public void getEarthquakeDatas() {
        BaseView.getExec().execute(new Runnable()
            {
                public void run() {
                    try {
                        datas=new ArrayList<String>();
                        names=new ArrayList<String>();
                        Document doc=Jsoup.connect("http://www.ceic.ac.cn/speedsearch").get();
                        Element ele=doc.getElementById("speed-search");
                        Elements ele3=ele.select("td[style]");
                        for(Element e:ele3){
                            levels.add(e.text());
                        }
                        Elements ele2=ele.select("a[href]");
                        for (Element e:ele2) {
                            datas.add(e.attr("href"));
                            names.add(e.text());
                            Blog.d(getTAG(),e.attr("href"));
                        }
                        setDatas(names,datas);
                    } catch (Exception e) {
                        Blog.d(getTAG(),"获取地震信息失败");
                        Log.e(TAG, "Get earthquake failure",e);
                    }
                }
            });
    }

    private void setDatas(final List<String> names, final List<String> links) {
        if (names!=null&&links!=null) {
            final MAdapter adapter=new MAdapter();
            runOnUiThread(new Runnable(){
                    public void run() {
                        if (names.size()!=0&&links.size()!=0) {
                            earth_listview.setAdapter(adapter);
                            earth_listview.setOnItemClickListener(new OnItemClickListener(){
                                    @Override
                                    public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4) {
                                        final String data=datas.get(p3);
                                        BaseView.getExec().execute(new Runnable()
                                            {
                                                public void run() {
                                                    List<String> ss=getMoreInfo(data);
                                                    if (ss!=null&&ss.size()>0) {
                                                        Intent intent=new Intent(EarthquakeActivity.this,EarthquakeDataActivity.class);
                                                        intent.putExtra("time",ss.get(0));
                                                        intent.putExtra("situated",ss.get(1));
                                                        intent.putExtra("longitude",ss.get(2));
                                                        intent.putExtra("deep",ss.get(3));
                                                        intent.putExtra("level",ss.get(4));
                                                        intent.putExtra("location",ss.get(5));
                                                        startActivity(intent);
                                                    } else {
                                                        toast("获取信息失败");
                                                    }
                                                }
                                            });
                                    }
                                });
                        }
                        else{
                            runOnUiThread(new Runnable()
                                {
                                    @Override
                                    public void run() {
                                        BigTitle.setText("没有地震");
                                    }
                                });
                        }
                    }
                });
        } else {
            runOnUiThread(new Runnable()
                {
                    @Override
                    public void run() {
                        BigTitle.setText("没有地震");
                    }
                });
        }
    }

    public List<String> getMoreInfo(final String url) {
        try {
            /*
             *0：时间
             *1：纬度
             *2：经度
             *3：深度
             *4：震级
             *5：地区
             */
            List<String> data=new ArrayList<String>();
            Document doc=Jsoup.connect(url).get();
            Element ele=doc.getElementById("title-content");
            Elements ele2=ele.select("tr");
            Elements ele3=ele2.select("td");
            for (int i=0;i < ele3.size();i++) {
                Element e=ele3.get(i);
                data.add(e.text());
            }
            return data;
        } catch (Exception e) {
            Log.e(getTAG(), "获取更多地震信息失败",e);
            return null;
        }
    }

    public String getTAG() {
        return TAG;
    }

    public void setTAG(String TAG) {
        this.TAG = TAG;
    }

    class MAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int p1) {
            return p1;
        }

        @Override
        public long getItemId(int p1) {
            return p1;
        }

        @Override
        public View getView(int p1, View p2, ViewGroup p3) {
            String name=names.get(p1);
            String data=levels.get(p1);
            @SuppressLint({"ViewHolder", "InflateParams"})
            View view=LayoutInflater.from(EarthquakeActivity.this).inflate(R.layout.earthquake_item,null);
            RelativeLayout ll=view.findViewById(R.id.earthquake_bg);
            ll.setBackgroundResource(R.drawable.main_cardview_bg_black);
            MTextView t1=view.findViewById(R.id.earthquake_title);
            MTextView t2=view.findViewById(R.id.earthquake_level);
            t2.setText(String.format("%s级", data));
            t1.setText(name);
            return view;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            datas.removeAll(datas);
            names.removeAll(names);
        } catch (Exception e) {
            finish();
        }
    }

}
